package org.footballmanager.pandafootball.system.controller;

import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.common.exception.ExceptionEnum;
import org.footballmanager.pandafootball.common.exception.MyException;
import org.footballmanager.pandafootball.system.domain.Excel;
import org.footballmanager.pandafootball.system.domain.FootPerson;
import org.footballmanager.pandafootball.system.domain.enums.JNFL;
import org.footballmanager.pandafootball.system.form.impl.FootPersonSearchForm;
import org.footballmanager.pandafootball.system.service.FootPersonService;
import org.footballmanager.pandafootball.system.service.FootTableTitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@SessionAttributes("footPersonSearchForm")
@RequestMapping("/foot/footPerson")
public class FootPersonController extends AddAndEditController<FootPerson> {
    /* 实体名称 */
    private static final String ENTITYNAME = "footPerson";
    private static final String ADDPAGE = String.format(Constants.ADDPAGE, Constants.FOOT, ENTITYNAME);
    private static final String EDITPAGE = String.format(Constants.EDITPAGE, Constants.FOOT, ENTITYNAME);
    private static final String LISTPAGE = String.format(Constants.LISTPAGE, Constants.FOOT, ENTITYNAME);
    private static final String VIEWPAGE = String.format(Constants.VIEWPAGE, Constants.FOOT, ENTITYNAME);
    private static final String REDIRECT = String.format(Constants.REDIRECT, Constants.FOOT, ENTITYNAME);
    private static final String EXCEL = "Excel";

    @Autowired
    private FootPersonService footPersonService;
    @Autowired
    private FootTableTitleService footTableTitleService;

    /**
     * 抽象方法
     *
     * @return 实体名称
     */
    @Override
    protected String getEntityName() {
        return ENTITYNAME;
    }

    /**
     * 初始化
     *
     * @param map                  参数
     * @param footPersonSearchForm 条件参数
     */
    @ModelAttribute
    public void init(Model map, @ModelAttribute FootPersonSearchForm footPersonSearchForm) {
        footPersonService.init(ENTITYNAME, footPersonSearchForm, map);
    }

    @RequestMapping(value = "/search")
    public String search(Model map, @ModelAttribute FootPersonSearchForm footPersonSearchForm) {
        footPersonService.search(ENTITYNAME, map, footPersonSearchForm);
        return LISTPAGE;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String delete(@RequestParam Long id, HttpServletRequest request) {
        footPersonService.delete(id, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/deleteall", method = RequestMethod.POST)
    public String deleteall(HttpServletRequest request, @ModelAttribute FootPersonSearchForm footPersonSearchForm) {
        footPersonService.deleteAll(footPersonSearchForm, request);
        return REDIRECT;
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String openAddPage(Model map) {
        map.addAttribute(ENTITYNAME, new FootPerson());
        return ADDPAGE;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public String openEditPage(Model map, @RequestParam Long id) {
        map.addAttribute(ENTITYNAME, footPersonService.get(id));
        return EDITPAGE;
    }

    @RequestMapping(value = "/view", method = RequestMethod.POST)
    public String openViewPage(Model map, @RequestParam Long id, @ModelAttribute FootPersonSearchForm footPersonSearchForm) {
        map.addAttribute("basicSkills", footTableTitleService.findByTableNameAndJnfl(Constants.PERSONTABLE, JNFL.BASIC));
        map.addAttribute("linkageSkills", footTableTitleService.findByTableNameAndJnfl(Constants.PERSONTABLE, JNFL.LINKAGE));
        map.addAttribute(ENTITYNAME, footPersonService.get(id));
        map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), footPersonSearchForm);
        return VIEWPAGE;
    }

    @RequestMapping(value = "/importExcel", method = RequestMethod.GET)
    public String importExcel(Model map) {
        map.addAttribute(EXCEL, new Excel());
        return "foot/footPerson/ImportExcel";
    }

    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public String updateExcel(@ModelAttribute Excel excel, BindingResult bindingResult,
                              HttpServletRequest request, SessionStatus s) {
        if (excel.getImg() == null || excel.getImg().length == 0) {
            bindingResult(bindingResult);
            return "foot/footPerson/ImportExcel";
        }
        try {
            footPersonService.getExcel(excel.getImg(), request);
        } catch (IOException e) {
            throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
        }
        s.setComplete();
        return REDIRECT;
    }

    @RequestMapping(value = "/importSCardExcel", method = RequestMethod.GET)
    public String importSCardExcel(Model map) {
        map.addAttribute(EXCEL, new Excel());
        return "foot/footPerson/ImportSCardExcel";
    }

    @RequestMapping(value = "/importSCardExcel", method = RequestMethod.POST)
    public String updateSCard(@ModelAttribute Excel excel, BindingResult bindingResult,
                              HttpServletRequest request, SessionStatus s) {
        if (excel.getImg() == null || excel.getImg().length == 0) {
            bindingResult(bindingResult);
            return "foot/footPerson/ImportSCardExcel";
        }
        try {
            footPersonService.getSCardExcel(excel.getImg(), request);
        } catch (IOException e) {
            throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
        }
        s.setComplete();
        return REDIRECT;
    }

    @RequestMapping(value = "/importPCardExcel", method = RequestMethod.GET)
    public String importPCardExcel(Model map) {
        map.addAttribute(EXCEL, new Excel());
        return "foot/footPerson/ImportPCardExcel";
    }

    @RequestMapping(value = "/importPCardExcel", method = RequestMethod.POST)
    public String updatePCard(@ModelAttribute Excel excel, BindingResult bindingResult,
                              HttpServletRequest request, SessionStatus s) {
        if (excel.getImg() == null || excel.getImg().length == 0) {
            bindingResult(bindingResult);
            return "foot/footPerson/ImportPCardExcel";
        }
        try {
            footPersonService.getPCardExcel(excel.getImg(), request);
        } catch (IOException e) {
            throw new MyException(ExceptionEnum.INPUTEXCEPTION, e);
        }
        s.setComplete();
        return REDIRECT;
    }

    /**
     * 菜品时段销售表数据导出
     *
     * @param request  请求
     * @param response 响应
     */
    @RequestMapping(value = "/export", produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE, method = RequestMethod.GET)
    public void exportFoodIntervalData(HttpServletRequest request, HttpServletResponse response) {
        footPersonService.exprot(request, response);
    }

    private void bindingResult(BindingResult bindingResult) {
        bindingResult.rejectValue("img", Constants.MISFORMAT, "请选择上传Excel文件");
    }
}
